﻿package  de.wellenblau.helper
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	* ...
	* @author Markus Hesper
	*/
	public class scrollArea extends Sprite
	{
		private var _height:int;
		private var _width:int;
		private var _area:Sprite;
		private var _2scroll:Sprite;
		private var _vScrubber:Sprite;
		private var _hScrubber:Sprite;
		private var _dragVrect:Rectangle;
		private var _dragHrect:Rectangle;
		private var _barWidth:int;
		private var _scrubThick:int;
		private var _scrubColor:uint;
		private var _both:int;
		private var _stage:Stage;
		
		
		public function scrollArea(width:int, height:int,stage:Stage, scrubThickness:int=15,color:uint = 0xd2d2d2) 
		{
			_height = height;
			_width = width;
			_scrubThick = scrubThickness;
			_scrubColor = color;
			_both = 0;
			_stage = stage;
			
		}
		public function add2scroll(d:DisplayObject):void {
			if (d.width > this._width && d.height <= this._height) {
				_area = new Sprite();
				_area.graphics.beginFill(0xff00ff,0);
				_area.graphics.drawRect(this.x,this.y, _width, _height - _scrubThick);
				_area.graphics.endFill();
				_2scroll = new Sprite();
				_2scroll.addChild(d);
				_2scroll.mask = this._area;
				addChild(_2scroll);
				this.buildHscrubber();
			}
			else if(d.height > this._height && d.width <= this._width){
				_area = new Sprite();
				_area.graphics.beginFill(0xff00ff,0);
				_area.graphics.drawRect(this.x,this.y, _width - _scrubThick, _height);
				_area.graphics.endFill();
				
				_2scroll = new Sprite();
				_2scroll.addChild(d);
				_2scroll.mask = this._area;
				addChild(_2scroll);
				this.buildVscrubber();
			}
			else if(d.height > this._height && d.width > this._width){
				_both = _scrubThick;
				_area = new Sprite();
				_area.graphics.beginFill(0xff00ff,0);
				_area.graphics.drawRect(this.x,this.y, _width - _scrubThick, _height - _scrubThick);
				_area.graphics.endFill();
				
				_2scroll = new Sprite();
				_2scroll.addChild(d);
				_2scroll.mask = this._area;
				addChild(_2scroll);
				this.buildVscrubber();
				this.buildHscrubber();
			}
			else {
				_2scroll = new Sprite();
				_2scroll.addChild(d);
				this.addChild(_2scroll);
			}
		}
		public function startVD(e:Event):void {
			// trace('Start Drag');
			_vScrubber.startDrag(false, _dragVrect);
			_vScrubber.addEventListener(Event.ENTER_FRAME, onEnter);
		}
		public function stopVD(e:Event):void {
			// trace('Stop Drag');
			_vScrubber.stopDrag();
			_vScrubber.removeEventListener(Event.ENTER_FRAME, onEnter);
		}
		public function startHD(e:Event):void {
			// trace('Start Drag');
			_hScrubber.startDrag(false, _dragHrect);
			_hScrubber.addEventListener(Event.ENTER_FRAME, onEnter);
		}
		public function stopHD(e:Event):void {
			// trace('Stop Drag');
			_hScrubber.stopDrag();
			_hScrubber.removeEventListener(Event.ENTER_FRAME, onEnter);
		}
		public function buildVscrubber():void {
			//// trace('2scrollH: ' + _2scroll.height);
			_vScrubber = new Sprite();
			_vScrubber.buttonMode = true;
			_vScrubber.useHandCursor = true;
			_vScrubber.graphics.beginFill(_scrubColor);
			//Calculate the Size of the Scrubber 
			var scrubHeight:int = (_height - _both) * (_area.height / _2scroll.height);
			if (scrubHeight < _scrubThick) { scrubHeight = _scrubThick; }
			//Draw scrubber
			_vScrubber.graphics.drawRoundRect(_width - _scrubThick, 0, _scrubThick, scrubHeight,_scrubThick,_scrubThick);
			_vScrubber.graphics.endFill();
			this.addChild(_vScrubber);
			//Drag-Rectagle for the Scrubber
			_dragVrect= new Rectangle(0,0, 0, _height-_vScrubber.height-_both);
			//Add Drag Events
			_vScrubber.addEventListener(MouseEvent.MOUSE_DOWN, startVD);
			_stage.addEventListener(MouseEvent.MOUSE_UP, stopVD);
			
		}
		public function buildHscrubber():void {
			//// trace('2scrollH: ' + _2scroll.height);
			_hScrubber = new Sprite();
			_hScrubber.buttonMode = true;
			_hScrubber.useHandCursor = true;
			_hScrubber.graphics.beginFill(_scrubColor);
			//Calculate the Size of the Scrubber 
			var scrubWidth:int = (_width - _both) * (_area.width / _2scroll.width);
			if (scrubWidth < _scrubThick) { scrubWidth = _scrubThick; }
			//Draw scrubber
			_hScrubber.graphics.drawRoundRect(0,_height - _scrubThick,scrubWidth, _scrubThick, _scrubThick,_scrubThick);
			_hScrubber.graphics.endFill();
			this.addChild(_hScrubber);
			//Drag-Rectagle for the Scrubber
			_dragHrect= new Rectangle(0,0,_width-_hScrubber.width-_both ,0);
			//Add Drag Events
			_hScrubber.addEventListener(MouseEvent.MOUSE_DOWN, startHD);
			_stage.addEventListener(MouseEvent.MOUSE_UP, stopHD);
			
		}

		public function onEnter(e:Event ):void {
			if(e.target == _vScrubber){
				this._2scroll.y = (_2scroll.height - _area.height) * (_vScrubber.y / (_height - _vScrubber.height - _both)) * -1;
			}
			else if (e.target == _hScrubber) {
				this._2scroll.x = (_2scroll.width - _area.width) * (_hScrubber.x / (_width - _hScrubber.width - _both)) * -1;
			}
		}
		
	}
	
}